<?php
App::uses('AppModel', 'Model');
/**
 * TicketAdvisory Model
 *
 * @property Ticket $Ticket
 */
class TicketAdvisory extends AppModel {

	public $actsAs = array('Pagination');
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'ticket_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'question' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'question_ip' => array(
				'notEmpty' => array(
						'rule' => array('notEmpty'),
						//'message' => 'Your custom message here',
						//'allowEmpty' => false,
						//'required' => false,
						//'last' => false, // Stop validation after this rule
						//'on' => 'update', // Limit validation to 'create' or 'update' operations
				),
		),
		'answer' => array(
				'notEmpty' => array(
						'rule' => array('notEmpty'),
						//'message' => 'Your custom message here',
						//'allowEmpty' => false,
						//'required' => false,
						//'last' => false, // Stop validation after this rule
						'on' => 'update', // Limit validation to 'create' or 'update' operations
				),
		),
		'publish' => array(
			'datetime' => array(
				'rule' => array('datetime'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ticket_show' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Ticket' => array(
			'className' => 'Ticket',
			'foreignKey' => 'ticket_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
				'className' => 'User',
				'foreignKey' => 'user_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		),
		'Operator' => array(
				'className' => 'User',
				'foreignKey' => 'operator_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		)
	);
	
	
	/**
	 * 分页条件
	 * @author mingxing.zhao@qq.com
	 * @Date 2012-12-20
	 * @return Array
	 */
	function pageParam($action){
		return array(
				"colNames"=>array(
						'ID','票品名称','咨询问题','咨询人','咨询时间','编辑','显示到票品','创建时间','操作'
				),
				"colModel"=>array(
						array("name"=>'TicketAdvisory.id',"width"=>'50',"sortable"=>true,"align"=>"right"),
						array("name"=>'TicketAdvisory.ticket_id',"width"=>"200","sortable"=>true,"align"=>"left"),
						array("name"=>'TicketAdvisory.question',"width"=>"200","sortable"=>true,"align"=>"left"),
						array("name"=>'TicketAdvisory.user_id',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'TicketAdvisory.publish',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'TicketAdvisory.operator_id',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'TicketAdvisory.ticket_show',"width"=>"50","sortable"=>true,"align"=>"center",'formatter'=>'checkbox'),
						array("name"=>'TicketAdvisory.created',"width"=>"110","sortable"=>true,"align"=>"center"),
						array("name"=>'act',"width"=>"150","sortable"=>false,"align"=>"center")
				),
				"action"=>$action,
				'sortname'=>'id',
				'sortorder'=>'DESC'
		);
	}
	
	/**
	 * 分页数据
	 * @author mingxing.zhao@qq.com
	 * @param array
	 * @date 2013-12-20
	 * @return json
	 *
	 */
	function jsonData($res){
		$rowN = array();
		$i=0;
		foreach($res['rows'] as $k => $value){
			$rowN[$i]['id'] = $value['TicketAdvisory']['id'];
			$rowN[$i++]['cell'] = array(
					$value['TicketAdvisory']['id'],
					$value['Ticket']['title'],
					$value['TicketAdvisory']['question'],
					$value['User']['name']?$value['User']['name']:'网友',
					date('m-d',strtotime($value['TicketAdvisory']['publish'])),
					$value['Operator']['name']?$value['Operator']['name']:'<font color=orange>未处理</font>',
					$value['TicketAdvisory']['ticket_show'],
					$value['TicketAdvisory']['created'],
					''
			);
		}
		$res['rows'] = $rowN;
		return json_encode($res);
	}
}
